<?php
namespace app\api\controller\deerhome\shop;

use app\api\controller\deerhome\shop\BaseAuth;
use think\Db;
use think\Validate;
use app\admin\model\deerhome\Shop as ShopModel;
use app\admin\model\deerhome\Order as OrderModel;

class Home extends BaseAuth
{
    public function index()
    {
        $id=$this->request->param("id",0,"intval");
        $month=$this->request->param("month","","trim");
        $out=[];
        $out['icons']=[];
        //获取3天内的订单
        $day3=\date("Y-m-d",strtotime("+3 day"));
        array_push($out['icons'],[
            "name"=>"3天内到店"
            ,"num"=>OrderModel::where("order_way",2)->where("status",3)->where("deerhome_shop_id",$id)->where("date_day","<=",$day3)->count()
            ,"icon"=>"cuIcon-notice"
            ,"color"=>"#e54d42"
            ,"url"=>""
        ]);
        array_push($out['icons'],[
            "name"=>"累计待核销"
            ,"num"=>OrderModel::where("order_way",2)->where("status",3)->where("deerhome_shop_id",$id)->count()
            ,"icon"=>"cuIcon-time"
            ,"color"=>"#0081ff"
            ,"url"=>""
        ]);
        array_push($out['icons'],[
            "name"=>"逾期未核销"
            ,"num"=>OrderModel::where("order_way",2)->where("status",3)->where("deerhome_shop_id",$id)->where("date_day","<",date('Y-m-d'))->count()
            ,"icon"=>"cuIcon-warn"
            ,"color"=>"#fbbd08"
            ,"url"=>""
        ]);
        array_push($out['icons'],[
            "name"=>"今日下单"
            ,"num"=>OrderModel::where("order_way",2)->where("status","in",[3,4,5])->where("deerhome_shop_id",$id)->whereTime("time_add","today")->count()
            ,"icon"=>"cuIcon-goodsfavor"
            ,"color"=>"#8799a3"
            ,"url"=>""
        ]);
        array_push($out['icons'],[
            "name"=>"昨日下单"
            ,"num"=>OrderModel::where("order_way",2)->where("status","in",[3,4,5])->where("deerhome_shop_id",$id)->whereTime("time_add","yesterday")->count()
            ,"icon"=>"cuIcon-goodsfavor"
            ,"color"=>"#8799a3"
            ,"url"=>""
        ]);

        $out['calendar']=[];
        $month=empty($month)?\date("Y-m"):$month;
        $star_day=\date("Y-m-01",strtotime($month));
        $end_day=\date("Y-m-t",strtotime($month));
        $orderNumArr=OrderModel::where("order_way",2)->where("status",3)->where("deerhome_shop_id",$id)->where("date_day",">=",$star_day)->where("date_day","<=",$end_day)->field("date_day,count(id) as numCount")->group('date_day')->select();
        $orderNumArrFormat=[];
        foreach($orderNumArr as $v){
            $orderNumArrFormat[$v['date_day']]=$v['numCount'];
        }
        for($i=0;$i<\date("t");$i++){
            $day=\date("Y-m-d",strtotime($star_day." +".$i." day"));
            if(isset($orderNumArrFormat[$day])){
                array_push($out['calendar'],[
                    "text"=>$orderNumArrFormat[$day]."单"
                    ,"value"=>$day
                    ,"bgColor"=>"#ff7900"
                    ,"color"=>"#ffffff"
                    ,"check"=>false
                ]);
            }else{
                array_push($out['calendar'],[]);
            }
        }
        
        $this->ok($out);
    }
}
